package mvc.common;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;

public class BaseDao {
    private static String driverClass;
    private static String url;
    private static String name;
    private static String password;
    private static Connection conn = null;
    private static PreparedStatement ps = null;
    private static ResultSet rs = null;

    static {
        driverClass = "com.mysql.jdbc.Driver";
        url = "jdbc:MySQL://127.0.0.1:3306/yf1805?useSSL=false";
        name = "root";
        password = "123456";
    }

    private static Connection getConn(){
        conn = null;
        try {
            Class.forName(driverClass);
            conn =  DriverManager.getConnection(url, name, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 释放资源
     */
    private static void release(){
        closeRs(rs);
        closeSt(ps);
        closeConn(conn);
    }

    /**
     * 运用反射直接创建对象
     * 查询数据库
     * @param sql sql语句
     * @param clz 查询对象的.class文件
     * @param obj 不定长参数
     * @param <T> 返回的对象类型
     * @return 返回一个列表
     */
    public synchronized  <T> List<T> query(String sql, Class<T> clz, Object... obj) {
        List<T> list = new LinkedList<>();
        conn = getConn();
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                ps.setObject(i + 1, obj[i]);
            }
            rs = ps.executeQuery();
            while (rs.next()) {
                //通过反射拿到储存结构里面的所有属性
                Field[] files = clz.getDeclaredFields();
                //通过反射使用无参构造器生成一个对象
                T t = clz.getConstructor().newInstance();
                for (Field file : files) {
                    //设置可以对私有属性进行直接更改
                    file.setAccessible(true);
                    //将t对象的属性设置成数据库里拿出来的数据
                    file.set(t, rs.getObject(file.getName()));
                }
                list.add(t);
            }
        } catch (SQLException | InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
            e.printStackTrace();
        }finally {
            release();
        }
        return list;
    }

    /**
     * 接口原理
     * 查询数据库
     * @param sql sql语句
     * @param mapper 接口的类型
     * @param obj 参数
     * @param <T> 返回的对象类型
     * @return 返回一个列表
     */
    public <T> List<T> query(String sql, RowMapper<T> mapper, Object... obj) {
        List<T> list = new LinkedList<>();
        conn = getConn();
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                ps.setObject(i + 1, obj[i]);
            }
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(mapper.rowmap(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            release();
        }
        return list;
    }

    /**
     * 修改数据库数据
     * @param sql sql语句
     * @param obj 参数
     */
    public int update(String sql, Object... obj) {
        conn = getConn();
        int count = 0;
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                ps.setObject(i + 1, obj[i]);
            }
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            release();
        }
        return count;
    }

    /**
     * 关闭所有ResultSet
     * @param rs ResultSet对象
     */
    private static void closeRs(ResultSet rs){
        try {
            if(rs != null){
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            rs = null;
        }
    }

    /**
     * 关闭Statement
     * @param st st兑现
     */
    private static void closeSt(Statement st){
        try {
            if(st != null){
                st.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            st = null;
        }
    }

    /**
     * 关闭Connection
     * @param conn conn对象
     */
    private static void closeConn(Connection conn){
        try {
            if(conn != null){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            conn = null;
        }
    }
}
